*Cameron, Kastellec, and Mattioli Replication

************************************************
*DEPENDENCIES
************************************************
*Uncomment to install estout from replication folder 
*copy "~/CKM_Replication/packages/estout.ado" "'c(sysdir_plus)'"
*discard

*Uncomment to install estout from ssc repository 
*ssc install estout, replace

*Alternative: (uncomment)
*net install st0085_2, replace

*Uncomment to install more_clarify from replication folder 
*copy "~/CKM_Replication/packages/more_clarify.ado" "'c(sysdir_plus)'"
*discard

*Uncomment to install more_clarify from ssc repository
*ssc install more_clarify, replace

************************************************
*LOAD DATA
************************************************
cd "~/CKM_Replication"
clear
set more off
use "full_short_list_data_for_characteristics.dta", clear

************************************************
*IDEOLOGY
************************************************
*do 2 sets of regressions -- 1st using number of co-partisans as costs, then percent same party presidents
*define vars
gen p_i = pres_dwnom1
*define interests (pi_i) here
	*platform index = policy_platform_index
	*pca of platform/lauren speeches == pres_interest_pca 
gen pi_x = pres_interest_pca 
gen log_pi_x = log(pi_x)
*define costs (pi) here
gen w_x_copar = 1/coa_n_copartisans
gen log_w_x_copar = log(w_x)
*define ratio 
gen w_over_pi_x_copar =  w_x_copar / (pi_x + .01) /*add .01 to account for if using pure platform measure */
gen log_w_over_pi_x_copar =  log(w_over_pi_x_copar)
*reg w_over_pi_x  coa_n_copartisans
gen abs_president = abs(p_i)
*need to redine x-bar to account for liberals
gen x_bar = noms_sc_mean_NSP if dem_president==0
replace x_bar = -1*noms_sc_mean_NSP if dem_president==1
*define x variable
gen x_hat = cand_dwnom_new if dem_president==0
replace x_hat = -1*cand_dwnom_new if dem_president==1
*define x-naught
gen x_0 = coa_mean_ghp_DW_overall
 
gen x_star = .
*REPUBLICANS
replace x_star = 0 if cand_dwnom_new <= x_0 & dem_president==0
replace x_star = cand_dwnom_new-x_0 if cand_dwnom_new > x_0 & dem_president==0
*DEMOCRATS
replace x_star = 0 if cand_dwnom_new >= x_0 & dem_president==1
replace x_star = x_0-cand_dwnom_new if cand_dwnom_new < x_0 & dem_president==1

************************************************************************************************************
*REGRESSION MODELS FOR TABLE 2 (ideology1, ideology2, etc correspond to the 8 models in the Table)
************************************************************************************************************

*TOBITS
	*1st: all short list candidates
eststo ideology1: tobit  x_star  w_over_pi_x_copar, ll(0) cluster(nominee)
*fitstat
eststo ideology2: tobit  x_star  w_over_pi_x_copar abs_president x_bar x_0, ll(0) cluster(nominee)
*fitstat

predict ideo_policy_prob_from_sl_model, pr(0,.)
postsim, saving(ideology_sims, replace) seed(1010): tobit  x_star  w_over_pi_x abs_president x_bar x_0, ll(0) cluster(nominee)

	*2nd just nominees:
eststo ideology3: tobit x_star  w_over_pi_x if nominated==1, ll(0)
*eststo ideology5: tobit x_star  w_over_pi_x abs_president if nominated==1, ll(0)
eststo ideology4: tobit x_star  w_over_pi_x abs_president x_bar  x_0 if nominated==1,ll(0)
**predictions 
predict ideo_policy_prob_from_nom_model, pr(0,.)/*set bounds from zero to infinity*/
plot ideo_policy_prob_from_nom_model year_announced
predict ideo_latent, xb

********************
********************
*now re-do costs using Senate support/opposition
gen w_x_senate = 1/percent_sameparty_senators
gen log_w_x_senate = log(w_x_senate)
*define ratio 
gen w_over_pi_x_senate =  w_x_senate / (pi_x + .01) /*add .01 to account for if using pure platform measure */
gen log_w_over_pi_x_senate =  log(w_over_pi_x_senate)

eststo ideology5: tobit  x_star  w_over_pi_x_senate , ll(0) cluster(nominee)
eststo ideology6: tobit  x_star  w_over_pi_x_senate abs_president x_bar x_0, ll(0) cluster(nominee)

	*2nd all nominees:
eststo ideology7: tobit x_star  w_over_pi_x_senate if nominated==1, ll(0)
*eststo ideology11: tobit x_star  w_over_pi_x abs_president if nominated==1, ll(0)
eststo ideology8: tobit x_star  w_over_pi_x_senate abs_president x_bar x_0 if nominated==1,ll(0)

*Make regression table (Table 2 in paper)
esttab ideology1 ideology2 ideology3 ideology4 ideology5 ideology6 ideology7 ideology8 using "ideology_regressions_rr.tex", nostar /// 
 	replace cells("b(fmt(3))" "se(par)") stats(N r2 pr2 ar2)  style(tex)  starlevels( * 0.05  ) ///
	title(Ideology regressions\label{ideologyregressions})

****************************************
*RELIABILITY
****************************************
// dv = cand reliability reliable_cand_binary_squared
// q0 = coa_mean_reliability_index 
	*coa_copartisan_reliability  = copartisans
	*coa_mean_reliability_index  = overall
 
// w = cost of finding reliabily = number of co-partisan = coa_n_copartisans
// pi == pres interest in policy = platform index (policy_platform_index) + lauren speeches

*define q_0 and q_hat
gen q_0 = coa_mean_reliability_index 
*coa_mean_reliability_index
gen q_hat = reliable_cand_binary

*dv --define censoring
gen q_star = .
replace q_star  =  q_hat - q_0   if q_hat > q_0
replace q_star  =  0  if q_hat <= q_0

// gen qq = reliable_cand_binary - coa_mean_reliability_index
// graph7 qq  coa_mean_reliability_index

*define interest/cost term here
	*platform index = policy_platform_index
	*pca of platform/lauren speeches == pres_interest_pca 
gen pi_q = pres_interest_pca 
gen w_q = 1/coa_n_copartisans

*need to normalize for root term 
gen inner_term = pi_q / w_q
gen root_pi_w = sqrt(inner_term)

************************************************************************************************************
*REGRESSION MODELS FOR TABLE 3 (reliability1, reliability2, etc correspond to the 6 models in the Table)
************************************************************************************************************

eststo reliability1: tobit q_star root_pi_w , ll(0) cluster(nominee)
eststo reliability2: tobit q_star root_pi_w  if nominated==1 , ll(0)
eststo reliability3: tobit q_star root_pi_w q_0, ll(0) cluster(nominee)
eststo reliability4: tobit q_star root_pi_w  q_0 if nominated==1, ll(0)

**predictions
predict rel_prob_from_nom_model, pr(0,.)/*set bounds from zero to infinity*/
plot rel_prob year_announced
predict rel_latent, xb

*now use logs
	*can't run tobit because censoring point changes
gen dv_log = log(q_star + q_0)
gen pi_log = log( 1 + pi_q)
gen w_log = log(1/coa_n_copartisans)

eststo reliability5: reg dv_log pi_log w_log, cluster(nominee)
eststo reliability6: reg dv_log pi_log w_log if nominated ==1

*Make regression table (Table 3 in paper)
	*note: this creates a Tex file, but the table is modified manually in the paper TeX file
esttab reliability1 reliability2 reliability3 reliability4 reliability5 reliability6 using "reliability_regressions_rr.tex", /// 
	replace cells("b(fmt(3))" "se(par)") stats(N r2 r2_p ) style(tex) starlevels(  * 0.05 ) ///
	title(Reliability regressions\label{reliabilityregressions})

********
*DIVERSITY
********
*cand_div_index = index of candidate diversity
*coa_mean_diversity_index = mean level of co-partisan diversity
*platform_diverse_interest == platforms (could also bring in house membership)
*extant_div_index == mean # of minorities on Court

*For now, assume y^0 is white male (so drops out), and just use binary dv
gen y_hat = cand_div_index >0
 *define interest here:
gen pi_y =platform_diverse_index 
gen w_y = 1/ (.01+coa_nwm_copartisans)
gen y_bar = extant_div_index 
gen w_over_pi_y  = pi_y /w_y


************************************************************************************************************
*REGRESSION MODELS FOR TABLE 4 (diversity1, diversity2, etc correspond to the 6 models in the Table)
************************************************************************************************************

*models 1 and 2--bivariate
eststo diversity1: logit y_hat w_over_pi_y , nolog cluster(nominee)
eststo diversity2: logit y_hat w_over_pi_y  if nominated==1, nolog 
*models 3 and 4-add y-bar
eststo diversity3: logit y_hat w_over_pi_y y_bar, nolog cluster(nominee)
eststo diversity4: logit y_hat w_over_pi_y y_bar  if nominated==1, nolog 
predict div_prob_from_nom_model, pr

**predictions for counter-factual analysis
postsim, saving(diversity_sims_from_logit, replace) seed(100): logit y_hat w_over_pi_y y_bar if nominated==1

*With logs
drop dv_log pi_log w_log inner_term
gen inner_term =  1+y_hat  + 8*y_bar
gen dv_log = log(1+y_hat  + 8*y_bar) /*this assumes c= 8 and d=1 */
gen pi_log = log( pi_y + 1)
gen w_log = log( 1/ (1+w_y))

	*all candidates
eststo diversity5: reg dv_log pi_log w_log, cluster(nominee) 
	* [just nominees]
eststo diversity6: reg dv_log pi_log w_log if nominated==1

esttab diversity1 diversity2 diversity3 diversity4  diversity5 diversity6 using  "diversity_regressions_rr.tex", /// 
	replace cells("b(fmt(3))" "se(par)") stats(N r2)  starlevels( * 0.05 )  pr2 aic bic ///
	title(Diversity regressions\label{diversityregressions})

order nominee 
sort date*

save "tobit_postestimates_for_R.dta", replace




